﻿using Heroius.Extension;
using System.Collections.ObjectModel;
using System.Linq;

namespace AlbumDesign
{
    public static class GroupedPool
    {

        public static void AddPhoto(this ObservableCollection<PhotoGroup> groups, Photo photo)
        {
            PhotoGroup tar;
            if (groups.Contains(g => g.ID == photo.Group))
            {
                tar = groups.First(g => g.ID == photo.Group);
            }
            else
            {
                tar = new PhotoGroup() { ID = photo.Group };
                groups.Add(tar);
            }
            tar.Photos.Add(photo);
        }

        public static void RemovePhoto(this ObservableCollection<PhotoGroup> groups, Photo photo)
        {
            if (groups.Contains(g => g.ID == photo.Group))
            {
                var tar = groups.First(g => g.ID == photo.Group);
                tar.Photos.Remove(photo);
                if (tar.Photos.Count == 0)
                {
                    groups.Remove(tar);
                }
            }
        }

        public static bool ContainsPhoto(this ObservableCollection<PhotoGroup> groups, Photo photo)
        {
            if (groups.Contains(g=>g.ID==photo.Group))
            {
                var tar = groups.First(g => g.ID == photo.Group);
                return tar.Photos.Contains(photo);
            }
            else  return false;
        }
    }
}
